package com.pro.addresslist.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.pro.addresslist.R;
import com.pro.addresslist.dal.bll.FriendshipBLL;
import com.pro.addresslist.dal.model.V_FriendBean;
import com.pro.addresslist.ui.adapter.CommonAdapter;
import com.pro.addresslist.ui.adapter.ViewHolder;
import com.pro.addresslist.ui.common.BaseActivity;

public class FriendsDetailActivity extends BaseActivity {
private ListView  friend_list;
private CommonAdapter<V_FriendBean> friend;
private List<V_FriendBean> list_item=new ArrayList<V_FriendBean>();
private Bundle bd;
private int position;
private static boolean  isShow ;  // 是否显示CheckBox标识

	@Override
	public int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_friend_detail;
	}
	public class ReceiveBroadCast extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
        	if(action.equals("DeleteFriendShip")){
        		String result = (String) intent.getSerializableExtra("result");
        		if(result.equals("succeed")){
        			
        			Toast.makeText(mContext, "删除成功！！",Toast.LENGTH_SHORT).show();
        			 list_item.remove(position);
        			 finish();
        		}
        		else{
        			Toast.makeText(mContext, "删除失败！！",Toast.LENGTH_SHORT).show();
        			
        		}
        		friend.notifyDataSetChanged();
        	}
		}
		
		
	}
	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Intent intent=getIntent();
		bd=intent.getExtras();
		list_item=(List<V_FriendBean>) bd.getSerializable("friends");
		
		friend_list=(ListView) findViewById(R.id.list_friends);
		//Log.i("gj",list_item.get(0).get_Name().toString());
		friend=new CommonAdapter<V_FriendBean>(mContext, list_item, R.layout.activity_frienddetail_item) {
			
			@Override
			public void convert(ViewHolder holder, V_FriendBean t) {
				// TODO Auto-generated method stub
				holder.setText(R.id.frienddetail_name, t.get_Name()+"   "+t.get_MobileEn());
			}
		};
		
		friend_list.setAdapter(friend);
		
		
		//长按多选删除
		friend_list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				position=arg2;
				friend_list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
					@Override
					public void onCreateContextMenu(ContextMenu arg0, View arg1,
							ContextMenuInfo arg2) {
					
						MenuInflater inflator=new MenuInflater(FriendsDetailActivity.this);
						inflator.inflate(R.menu.relation_menu, arg0);
					}
				});
				return false;
			}
		});
				
				
		friend_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}
		});
		ReceiveBroadCast receiveBroadCast = new ReceiveBroadCast(); 
        IntentFilter filter = new IntentFilter(); 
        filter.addAction("DeleteFriendShip");
        registerReceiver(receiveBroadCast, filter); 
		}


	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.del:
			Toast.makeText(mContext,"选中值的ID为："+ list_item.get(position).get_ContactID(),Toast.LENGTH_SHORT).show();
			FriendshipBLL.DeleteFriendShip(mContext,list_item.get(position).get_ContactID());
			break;

		default:
			break;
		}
	
		return super.onContextItemSelected(item);
		
	}

	@Override
	public void OnViewClick(View v) {
		// TODO Auto-generated method stub
		
	}
	

}
